package com.zuo.mall.service.impl;

import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zuo.mall.common.api.CommonPage;
import com.zuo.mall.dto.PmsProductCategoryWithChildrenItem;
import com.zuo.mall.entity.PmsProduct;
import com.zuo.mall.entity.PmsProductCategory;
import com.zuo.mall.mapper.PmsProductCategoryMapper;
import com.zuo.mall.service.PmsProductCategoryService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PmsProductCategoryServiceImpl
        extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory>
        implements PmsProductCategoryService {
    @Override
    public List<PmsProductCategoryWithChildrenItem> listWithChildren() {
//
//        // 查询所有分类
//        List<PmsProductCategory> pmsProductCategories = baseMapper.selectList(null);
//        // 父级分类
//        List<PmsProductCategoryWithChildrenItem> parentProductCategories = pmsProductCategories.stream().filter(pmsProductCategory -> {
//            return pmsProductCategory.getLevel() < 1;
//        }).map(pmsProductCategory -> {
//            PmsProductCategoryWithChildrenItem parent = new PmsProductCategoryWithChildrenItem();
//            BeanUtils.copyProperties(pmsProductCategory, parent);
//            return parent;
//        }).collect(Collectors.toList());
//
//        // 子集分类
//        pmsProductCategories.removeAll(parentProductCategories);
//        if (Collections.isEmpty(parentProductCategories)) {
//            return null;
//        }
//
//        for (PmsProductCategoryWithChildrenItem parent : parentProductCategories) {
//            ArrayList<PmsProductCategory> childList = new ArrayList<>();
//            for (PmsProductCategory child : pmsProductCategories) {
//                if (parent.getId().equals(child.getParentId())) {
//                    childList.add(child);
//                }
//            }
//            parent.setChildren(childList);
//        }
//
//        return parentProductCategories;

        return baseMapper.listWithChildren();
    }

    @Override
    public CommonPage<PmsProductCategory> getList(Long parentId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);

        int start = PageUtil.getStart(pageNum - 1, pageSize);

        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductCategory::getParentId, parentId);
        Integer total = baseMapper.selectCount(queryWrapper);
        queryWrapper.last("LIMIT " + start+ ","+ pageSize);
        List<PmsProductCategory> pmsProductCategories = baseMapper.selectList(queryWrapper);

        CommonPage<PmsProductCategory> commonPage = new CommonPage<>();
        commonPage.setList(pmsProductCategories);
        commonPage.setTotal(Long.valueOf(total));
        commonPage.setTotalPage(PageUtil.totalPage(total, pageSize));
        return commonPage;
    }
}
